#include "stdafx.h"
#include "jryjleditor.h"
#include "jdbmodule.h"
#include "strhelper.h"
#include "wxhelper.h"
#include <fstream>
#include "jbleditor.h"

JRYJLEditor::JRYJLEditor(wxWindow * parent,const std::wstring & lsh)
    :JWebviewEditorUI(parent),m_lsh(lsh)
{
    //this->getWebView()->LoadURL(L"http://www.baidu.com/");
    //m_webview->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
        //And the memory: file system
    //m_webview->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));

    OnLoadScheme();
    this->m_webview->Connect(wxEVT_WEBVIEW_NAVIGATED, wxWebViewEventHandler( JRYJLEditor::OnWebViewNavigated), NULL, this );
}

JRYJLEditor::~JRYJLEditor()
{
    this->m_webview->Disconnect(wxEVT_WEBVIEW_NAVIGATED, wxWebViewEventHandler( JRYJLEditor::OnWebViewNavigated), NULL, this );
}
void JRYJLEditor::OnLoadScheme()
{
    wxPathList pathlist;
    pathlist.Add(L".");
    pathlist.Add(L"..");

    wxFileName helpfile(pathlist.FindValidPath(L"emrres.zip"));

    helpfile.MakeAbsolute();
    wxString path = helpfile.GetFullPath();
    //Under MSW we need to flip the slashes
    path.Replace(L"\\", L"/");
    path = L"wxfs:///" + path + L";protocol=zip/emrres/html/入院记录.html";

    m_webview->LoadURL(path);
}



void JRYJLEditor::OnToolClickedUpdate(wxCommandEvent &event)
{
    event.Skip(true);
    ado::ConnectionPtr con=ado::newHisCon();
    ado::CommandPtr cmd=ado::newCommand(con);
    cmd->PutCommandType(ADODB::adCmdText);
    static const wchar_t sql_t[]=
LR"(
SELECT LSH,RYXM AS 姓名,RYXB AS 性别,
       RYNL+isnull(NLDW,'岁') AS 年龄,CSDD AS 出生地,
       HYZK AS 婚姻,MZ AS 民族,ZY AS 职业,
       JTZZ AS 家庭住址,GZDW AS 工作单位,GSZ AS 供史者,
       GSZGX AS 供史者关系,convert(varchar(16),RYRQ,120) AS 入院日期,
       convert(varchar(16),SXRQ,120) AS 记录日期,
       ZS AS 主诉,XBS AS 现病史,JWS AS 既往史,GRS AS 个人史,
       JZS AS 家族史,TGJC_TW AS 体温,TGJC_MB AS 脉搏,
       TGJC_HX AS 呼吸,TGJC_SSY AS 收缩压,TGJC_SZY AS 舒张压,
       TGJC AS 体格检查,
       FZJC AS 辅助检查,ZKJC AS 专科情况,CBZD AS 初步诊断,
       CBZDYS AS 初诊医师,CBZDSJ AS 初诊时间,XZZD AS 修正诊断,
       XZZDYS AS 修诊医师,convert(varchar(10),XZZDSJ,23) AS 修诊日期
FROM [EMR_330400].[dbo].[ZYBL_RYDJ] AS RYDJ
WHERE LSH='%s'
)";
    std::wstring sql=str_format(sql_t,this->m_lsh.data());
    cmd->PutCommandText(_bstr_t(sql.data()));
    cmd->PutCommandType(ADODB::adCmdText);
    _variant_t nn;
    nn.vt=VT_I4;
    nn.tagVARIANT::lVal=0;


    ado::RecordsetPtr rs=cmd->Execute(&nn,NULL,ADODB::adOptionUnspecified);
    if(!rs->GetadoEOF())
    {
        std::wostringstream oss(std::ios::binary);
        oss<<L"$bl.setupTextOnDbkey({";
        ado::FieldsPtr fds=rs->GetFields();
        for(long i=0;i<fds->GetCount();++i)
        {
            ado::FieldPtr fd=fds->GetItem(i);
            _bstr_t fdn=fd->GetName();
            _variant_t fdv=fd->GetValue();
            wxVariant value;
            myConvertOleToVariant(fdv,value);

            if(value.IsNull())
            {
                oss<<L"\""<<(const wchar_t*)fdn<<L"\":null,";
            }else {
                std::wstring str=value.GetString().ToStdWstring();
                oss<<L"\""<<(const wchar_t*)fdn<<L"\":\""<<str_encode_to_jscode(str)<<"\",";
            }

        }
        oss<<"});";
        wxString jscode(oss.str());

        this->m_webview->RunScript(jscode);
    }

}

void JRYJLEditor::OnWebViewNavigated(wxWebViewEvent &event)
{
    JBLEditor * bledit=JBLEditor::fromLSH(m_lsh);
    std::wstring jscode=bledit->getJSCodeBRXX();
    this->m_webview->RunScript(jscode);

}
